//
//  HZBaseLogjcManager.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/5/25.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZBaseLogjcManager.h"

@interface HZBaseLogjcManager()
@property (strong, nonatomic) NSMutableDictionary *routes;
@end

@implementation HZBaseLogjcManager
//注册block
- (void)mapLogic:(NSString *)route toBlock:(LogicRouterBlock)block
{
    self.routes[route] = [block copy];
}
//匹配block
- (LogicRouterBlock)matchLogicBlock:(NSString *)route
{
    if (![self.routes.allKeys containsObject:route]) {
        return nil;
    }
    LogicRouterBlock block = self.routes[route];
    return block;
}

- (NSMutableDictionary *)routes
{
    if (!_routes) {
        _routes = [[NSMutableDictionary alloc] init];
    }
    
    return _routes;
}

- (void)dealloc{
    DLog(@"HZBaseLogjcManager销毁");
}

@end


@implementation UIView (LogicManager)

static char kAssociatedLogicPropertyObjectKey;
- (void)setLogicManager:(HZBaseLogjcManager *)logicManager
{
    objc_setAssociatedObject(self, &kAssociatedLogicPropertyObjectKey, logicManager, OBJC_ASSOCIATION_ASSIGN);
}
- (HZBaseLogjcManager *)logicManager
{
    return objc_getAssociatedObject(self, &kAssociatedLogicPropertyObjectKey);
}

- (void)didAddSubview:(UIView *)subview{
    if (!self.logicManager) {return;}
    subview.logicManager = self.logicManager;
    [self loopGetSubView:subview];
}
- (void)loopGetSubView:(UIView *)view
{
    if (view.subviews > 0) {
        for (UIView *subView in view.subviews) {
            subView.logicManager = view.logicManager;
            [self loopGetSubView:subView];
        }
    }
}

@end
